from django import forms
from django.core.exceptions import ValidationError
from user.models import VSession
import pdb


def token_check(value):
    # 验证token必须存在
    sessionList = VSession.objects.filter(token=value)
    if not sessionList.exists():
        errMsg = 'token Error'
        raise ValidationError(errMsg)
    else:
        session = sessionList[0]


def maxItem_check(value):
    if value < 1:
        errMsg = 'maxItem Error'
        raise ValidationError(errMsg)


def page_check(value):
    if value < 1:
        errMsg = 'page Error'
        raise ValidationError(errMsg)


class GetSoftNoticeForm(forms.Form):
    token = forms.CharField(max_length=100, validators=[token_check])
    page = forms.IntegerField(validators=[page_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])


class GetRunLogForm(forms.Form):
    token = forms.CharField(max_length=100, validators=[token_check])
    page = forms.IntegerField(validators=[page_check])
    maxItem = forms.IntegerField(validators=[maxItem_check])
    noticeType = forms.CharField(max_length=20)

    def clean(self):
        cleaned_data = super(GetRunLogForm, self).clean()
        noticeType = cleaned_data.get('noticeType')
        if noticeType not in ['0', '1']:
            errMsg = 'noticeType Error'
            raise ValidationError(errMsg)
        return self.cleaned_data


class GetReadForm(forms.Form):
    token = forms.CharField(max_length=100, validators=[token_check])
    id = forms.CharField(max_length=20)
    noticeType = forms.CharField(max_length=20)

    def clean(self):
        cleaned_data = super(GetReadForm, self).clean()
        noticeType = cleaned_data.get('noticeType')
        if noticeType not in ['0', '1','2','3']:
            errMsg = 'noticeType Error'
            raise ValidationError(errMsg)
        return self.cleaned_data
